package com.hjzd.ai.smartpractice.websocket;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * Websocket Config
 *
 * @author leiniao
 * @version 1.0.0
 */
@Configuration
@EnableWebSocket
public class WsConfig implements WebSocketConfigurer {
	@Resource
	private AudioStreamHandler audioStreamHandler;

	@Resource
	private WsHandShake wsHandShake;

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(audioStreamHandler, "/practice/AudioStreamBin")
				.addInterceptors(wsHandShake)
				.setAllowedOriginPatterns("*");
	}


	@Bean
	public ServletServerContainerFactoryBean createWebSocketContainer() {
		ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
		container.setMaxTextMessageBufferSize(1024000); // Adjust text buffer size if needed
		container.setMaxBinaryMessageBufferSize(1024000); // Adjust binary buffer size if needed
		return container;
	}
}
